package nero.yncc.com.createart.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityShopcar;
import nero.yncc.com.createart.bean.BeanShopCar;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.fragment.FragmentShopcar;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TextUtil;

/**
 * Created by nero on 17-6-23.
 */

public class AdapterShopCarOutSize_activity extends BaseAdapter {

    Context context;
    ArrayList<BeanShopCar> arrayList;

    ActivityShopcar fragmentShopcar;

    public AdapterShopCarOutSize_activity(Context context, ArrayList<BeanShopCar> arrayList, ActivityShopcar fragmentShopcar) {
        this.context = context;
        this.arrayList = arrayList;
        this.fragmentShopcar = fragmentShopcar;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final Holder holder;
        final BeanShopCar beanShopCar = arrayList.get(i);
        String name = beanShopCar.getName();
        switch (name) {
            case "组合套装":
            case "场景套装":
                if (view == null) {
                    view = LayoutInflater.from(context).inflate(R.layout.adapter_shopcar_outside, null);
                    view.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
                    holder = new Holder(view);
                    view.setTag(holder);
                } else {
                    holder = (Holder) view.getTag();
                }
                if (name.equals("组合套装")) {
                    holder.tv_name.setText("组合");
                    holder.tv_name2.setText("组合套装");
                } else {
                    holder.tv_name.setText("场景");
                    holder.tv_name2.setText("场景套装");
                }

                final ArrayList<BeanShopCar.goods> arrayList_goods = beanShopCar.getList();
                double price = 0, price_market = 0, sheng = 0;
                for (BeanShopCar.goods goods : arrayList_goods) {
                        price += Double.parseDouble(goods.getShop_price()) * goods.getNumber();
                        price_market += Double.parseDouble(goods.getMarket_price()) * goods.getNumber();
                }
                if (beanShopCar.isChoose()) {
                    holder.iv_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
                } else {
                    holder.iv_choose.setTextColor(context.getResources().getColor(R.color.textColorGray));
                }
                sheng = price_market - price;
                beanShopCar.setPrice(price);
                beanShopCar.setPrice_market(price_market);
                beanShopCar.setSheng(sheng);
                holder.tv_price.setText("￥" + price);
                holder.tv_price_market.setText("￥" + price_market);
                holder.tv_price_market.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                holder.tv_price.setText("￥" + price);
                holder.tv_sheng.setText("￥" + sheng);

                AdapterShopCarRecylerView_activity adapter_shopcar = new AdapterShopCarRecylerView_activity(context, arrayList_goods, fragmentShopcar);
//                holder.rv_list.
                holder.rv_list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                holder.rv_list.setAdapter(adapter_shopcar);
                break;
            case "单商品":
                if (view == null) {
                    view = LinearLayout.inflate(context, R.layout.adapter_shopcar, null);
                    view.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
                    holder = new Holder(view);
                    view.setTag(holder);
                } else {
                    holder = (Holder) view.getTag();
                }
                final BeanShopCar.goods godBean = arrayList.get(i).getList().get(0);

                holder.tv_money.setText("￥"+TextUtil.getText(godBean.getShop_price(), "0") + "");

                if (beanShopCar.isChoose()) {
                    holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
                } else {
                    holder.icon_choose.setTextColor(context.getResources().getColor(R.color.textColorGray_low));
                }
                holder.tv_name.setText(TextUtil.getText(godBean.getGoods_name(),"暂无名称"));
                holder.tv_shuxing.setText(TextUtil.getText(godBean.getSpec_name(),"暂无简介"));
                //点击选中
                holder.icon_choose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        beanShopCar.setChoose(!beanShopCar.isChoose());
                        AdapterShopCarOutSize_activity.this.notifyDataSetChanged();
                        fragmentShopcar.setMoney();
                    }
                });
                //点击选中
                holder.ll_root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        beanShopCar.setChoose(!beanShopCar.isChoose());
                        AdapterShopCarOutSize_activity.this.notifyDataSetChanged();
                        fragmentShopcar.setMoney();
                    }
                });


                //减少数量
                holder.tv_cut.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //减少
                        try {
                            int num = godBean.getNumber();
                            if (num > 1) {
                                num--;
                                godBean.setNumber(num);
                            }

                            holder.tv_num.setText(num + "");
                            holder.tv_xnum.setText("x " + num);

                            fragmentShopcar.setMoney();
                        } catch (Exception e) {
                        }
                    }
                });
                //增加数量
                holder.tv_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //减少
                        try {
                            int num = godBean.getNumber();

                            num++;
                            godBean.setNumber(num);


                            holder.tv_num.setText(num + "");
                            holder.tv_xnum.setText("x " + num);
                            fragmentShopcar.setMoney();
                        } catch (Exception e) {

                        }
                    }
                });

                ImageUtils.BindImage(context,holder.iv_image, godBean.getOriginal_img(),2);
                break;
        }
        return view;
    }


    class Holder extends RecyclerView.ViewHolder {
        IconView iv_choose;
        TextView tv_name, tv_name2, tv_price, tv_price_market, tv_sheng;
        RecyclerView rv_list;
        IconView icon_choose;
        ImageView iv_image;
        TextView tv_shuxing;
        TextView tv_money;
        TextView tv_cut;
        TextView tv_num;
        TextView tv_add;
        TextView tv_del;
        TextView tv_xnum;
        RelativeLayout ll_root;


        public Holder(View itemView) {
            super(itemView);
            tv_name2 = itemView.findViewById(R.id.tv_name2);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_price_market = itemView.findViewById(R.id.tv_price_market);
            tv_sheng = itemView.findViewById(R.id.tv_sheng);
            rv_list = itemView.findViewById(R.id.rv_list);
            icon_choose = itemView.findViewById(R.id.icon_choose);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_shuxing = itemView.findViewById(R.id.tv_shuxing);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_cut = itemView.findViewById(R.id.tv_cut);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_add = itemView.findViewById(R.id.tv_add);
            tv_del = itemView.findViewById(R.id.tv_del);
            ll_root = itemView.findViewById(R.id.ll_root);
            tv_xnum = itemView.findViewById(R.id.tv_xnum);
        }
    }
}
